package com.xuhzhiguang.youdao.cli;

public class Spinner extends Thread {

    private static String getSpinner(int state) {
        switch (state) {
            case 0:
                return "|";
            case 1:
                return "/";
            case 2:
                return "-";
            case 3:
                return "\\";
            default:
                return "";
        }
    }

    @Override
    public void run() {
        boolean running = true;
        int i = 0;
        while (running) {
            System.out.print("\r查询中  " + getSpinner(i));
            i = (i + 1) % 4;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            if (Thread.currentThread().isInterrupted()) {
                running = false;
                System.out.println("\033[2K");
            }
        }
    }
}
